<?php
class View_Administration_User_Pages extends View_Administration_User
{
	public function buildContent()
	{
		$request	= $this->registry->get( 'request' );
		$controller	= $this->registry->get( 'controller' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['viewPages'];

		$userId		= $request->get( 'userId' );
		$userRoles	= array();
		$roles		= Logic_Role::getUserRoles( FALSE, $userId );
		foreach( array_values( $roles ) as $roleTitle )
			$userRoles[]	= strtolower( str_replace( " ", "_", $roleTitle ) );
        $userRoles	= array_merge( $userRoles, array( 'inside', 'public' ) );
		$mainPages	= $controller->getPages( 'main' );

		$i		= 0;
		$list	= array();
		$pages		= array();
		$scope 	= 'links_main';
		foreach( $mainPages as $pageId => $pageData )
		{
			if( isset( $pageData['parent'] ) )
			{
				$parent	= $pageData['parent'];
		#		$scope	= "links_".$pageData['scope'];
				$label	= $pageId;
				if( isset( $this->words['main'][$scope][$pageId] ) )
					$label	= strip_tags( $this->words['main'][$scope][$pageId] );
				if( !array_intersect( $pageData['roles'], $userRoles ) )
					$label	= "<strike>".$label."</strike>";
				$item	= array();
				$roles	= array();
				$item['label']	= $label;
				$item	= $this->loadTemplate( 'administration.user.pages.listitem', $item );
				$pages[$parent]['pages'][]	= $this->html->ListItem( $item, 2, array( 'class' => 'sub' ) );
			}
			else
			{
				$pages[$pageId]['scope']	= $pageData['scope'];
				$pages[$pageId]['roles']	= $pageData['roles'];
			}
		}
		$list	= array();
		foreach( $pages as $mainPageId => $mainPageData )
		{
			$label	= $mainPageId;
			if( isset( $this->words['main'][$scope][$mainPageId] ) )
				$label	= strip_tags( $this->words['main'][$scope][$mainPageId] );
			$scope	= "links_".$mainPageData['scope'];
			$item	= array();
			$roles	= array();
			if( !array_intersect( $mainPageData['roles'], $userRoles ) )
				$label	= "<strike>".$label."</strike>";
			$item['pages']	= isset( $mainPageData['pages'] ) ? $this->html->unorderedList( $mainPageData['pages'] ) : "";
			$item['label']	= $label;
			$item	= $this->loadTemplate( 'administration.user.pages.listitem', $item );
			$list[]	= $this->html->ListItem( $item, 1, array( 'class' => 'main' ) );
		}
		$ui['list']		= $this->html->unorderedList( $list, 0, array( 'class' => 'pages' ) );

		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'edit' );
		$ui['heading']		= $words['heading'];
		return $this->loadTemplate( 'administration.user.pages.list', $ui );
	}
}
?>